﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DataStructure
{
    public class MySet
    {
        Hashtable data;
        public MySet()
        {
            data = new Hashtable();
        }

        public void Add(Object item)
        {
            if (!data.ContainsValue(item))
            {
                data.Add(Hash(item), item);
            }
        }

        public void Remove(Object item)
        {
            data.Remove(Hash(item));
        }

        public int Size()
        {
            return data.Count;
        }

        public MySet Union(MySet set)
        {
            MySet temptSet = new MySet();
            foreach (Object hashObject in data.Keys)
            {
                temptSet.Add(this.data[hashObject]);
            }

            foreach (Object hashObject in set.data.Keys)
            {
                if (!(this.data.ContainsKey(hashObject)))
                {
                    temptSet.Add(set.data[hashObject]);
                }
            }
            return temptSet;
        }

        public MySet Intersection(MySet set)
        {
            MySet temptSet = new MySet();

            foreach (Object hashObject in this.data.Keys)
            {
                if (set.data.ContainsKey(hashObject))
                {
                    temptSet.Add(set.data[hashObject]);
                }
            }
            return temptSet;
        }

        public bool Subset(MySet aSet)
        {
            if (this.data.Count > aSet.data.Count)
                return false;
            else
                foreach (Object key in this.data.Keys)
                    if (!(aSet.data.Contains(key)))
                        return false;
            return true;
        }


        public MySet Difference(MySet aSet)
        {
            MySet tempSet = new MySet();
            foreach (Object hashObject in data.Keys)
                if (!(aSet.data.ContainsKey(hashObject)))
                    tempSet.Add(data[hashObject]);
            return tempSet;
        }

        public override string ToString()
        {
            string s = null;
            foreach (Object key in data.Keys)
                s += data[key] + " ";
            return s;
        }
        
        private static string Hash(object item)
        {
            char[] chars;
            string s = item.ToString();
            chars = s.ToCharArray();
            int hashValue = 0;
            for (int i = 0; i < chars.GetUpperBound(0); i++)
            {
                hashValue += (int)chars[i];
            }

            return hashValue.ToString();
        }


    }
}
